package com.example.idachuappone.cook.item;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.cook.action.CookAction;
import com.example.idachuappone.cook.adapter.CookTwoAdapter;
import com.example.idachuappone.cook.entity.Cook;
import com.example.idachuappone.cook.entity.CookResult;
import com.example.idachuappone.ui.OnRefreshListener;
import com.example.idachuappone.ui.RefreshListView;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class SearchCookListActivity extends Activity implements OnClickListener, OnItemClickListener {
	private CookTwoAdapter adapter;
	private BitmapUtils bu;
	private Cook cook;
	private CookAction cookAction;
	private Dialog dialogLoading;
	private EditText et_search_content;
	private ImageView img_et_content_clear;
	private LinearLayout ll_no_data;
	private LinearLayout ll_no_network;
	private RefreshListView lv_search_cook;
	private int page;
	private String paramCoordinate;
	public TextWatcher textWatcher;
	private TextView tv_clear;

	public SearchCookListActivity() {
		this.page = 1;
		this.textWatcher = new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (SearchCookListActivity.this.et_search_content.getText().length() > 0) {
					SearchCookListActivity.this.img_et_content_clear.setVisibility(0);
				} else {
					SearchCookListActivity.this.img_et_content_clear
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				}
			}
		};
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_cook_list);
		BaseActivity.activitys.add(this);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.paramCoordinate = getIntent().getStringExtra("paramCoordinate");
		this.cook = new Cook();
		this.cookAction = new CookAction();
		initView();
	}

	private void initView() {
		this.ll_no_network = (LinearLayout) findViewById(R.id.ll_no_network);
		this.ll_no_network.setOnClickListener(this);
		this.ll_no_data = (LinearLayout) findViewById(R.id.ll_no_data);
		this.tv_clear = (TextView) findViewById(R.id.tv_clear);
		this.tv_clear.setOnClickListener(this);
		this.img_et_content_clear = (ImageView) findViewById(R.id.img_et_content_clear);
		this.img_et_content_clear.setOnClickListener(this);
		this.et_search_content = (EditText) findViewById(R.id.et_search_content);
		this.et_search_content.addTextChangedListener(this.textWatcher);
		this.et_search_content.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == 66) {
					if (SearchCookListActivity.this.et_search_content.getText().toString().replace(" ", "").length() == 0) {
						MainToast.show(SearchCookListActivity.this, "\u8bf7\u8f93\u5165\u641c\u7d22\u5173\u952e\u5b57",
								0);
						return true;
					}
					SearchCookListActivity.this.lv_search_cook.requestFocusFromTouch();
					SearchCookListActivity.this.lv_search_cook.setSelection(0);
					SearchCookListActivity.this.page = 1;
					SearchCookListActivity.this.getData();
				}
				return false;
			}
		});
		this.lv_search_cook = (RefreshListView) findViewById(R.id.lv_search_cook);
		this.lv_search_cook.setOnItemClickListener(this);
		this.adapter = new CookTwoAdapter(this, this.bu);
		this.lv_search_cook.setAdapter(this.adapter);
		this.lv_search_cook.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onLoadMoring() {
				SearchCookListActivity searchCookListActivity = SearchCookListActivity.this;
				searchCookListActivity.page = searchCookListActivity.page + 1;
				SearchCookListActivity.this.addData(SearchCookListActivity.this.getUrl());
			}
		});
	}

	private void addData(String path) {
		NetUtil.get(this, path, 100, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				SearchCookListActivity.this.dialogLoading.dismiss();
				try {
					JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
					if (jsonObject.getInt("code") == 10000) {
						Cook cook1 = new Cook();
						cook1.parseJson(jsonObject.getJSONObject("data"));
						if (cook1.getResults() == null || cook1.getResults().size() <= 0) {
							SearchCookListActivity searchCookListActivity = SearchCookListActivity.this;
							searchCookListActivity.page = searchCookListActivity.page - 1;
							MainToast.show(SearchCookListActivity.this, "\u6682\u65e0\u6570\u636e", 0);
							SearchCookListActivity.this.lv_search_cook.hideFooterView(false);
							return;
						}
						SearchCookListActivity.this.cook.getResults().addAll(cook1.getResults());
						SearchCookListActivity.this.initAdapter(SearchCookListActivity.this.cook.getResults());
					} else if (jsonObject.getInt("code") == 300) {
						MainToast.show(SearchCookListActivity.this, jsonObject.getString("error"), 0);
					}
				} catch (JSONException e) {
					e.printStackTrace();
					MainToast.show(SearchCookListActivity.this,
							SearchCookListActivity.this.getResources().getString(R.string.netFail), 0);
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				SearchCookListActivity searchCookListActivity = SearchCookListActivity.this;
				searchCookListActivity.page = searchCookListActivity.page - 1;
				MainToast.show(SearchCookListActivity.this,
						SearchCookListActivity.this.getResources().getString(R.string.netFail), 0);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
		String result = this.cookAction.isCookService(this.cook.getResults().get(position));
		if (result == null) {
			Intent intent = new Intent(this, CookDetailActivity.class);
			intent.putExtra("id", this.cook.getResults().get(position).getId());
			startActivity(intent);
			return;
		}
		MainToast.show(this, result, 0);
	}

	private void getData() {
		if (CheckUtil.checkNet(this)) {
			this.dialogLoading.show();
			NetUtil.get(this, getUrl(), 100, new RequestCallBack<String>() {
				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					SearchCookListActivity.this.ll_no_network
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
					SearchCookListActivity.this.dialogLoading.dismiss();
					try {
						JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
						if (jsonObject.getInt("code") == 10000) {
							SearchCookListActivity.this.cook.parseJson(jsonObject.getJSONObject("data"));
							if (SearchCookListActivity.this.cook.getResults() == null
									|| SearchCookListActivity.this.cook.getResults().size() <= 0) {
								SearchCookListActivity.this.ll_no_data.setVisibility(0);
								return;
							}
							SearchCookListActivity.this.ll_no_data
									.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
							SearchCookListActivity.this.initAdapter(SearchCookListActivity.this.cook.getResults());
						} else if (jsonObject.getInt("code") == 300) {
							MainToast.show(SearchCookListActivity.this, jsonObject.getString("error"), 0);
						}
					} catch (JSONException e) {
						e.printStackTrace();
						MainToast.show(SearchCookListActivity.this, SearchCookListActivity.this.getResources()
								.getString(R.string.app_error), 0);
					}
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					SearchCookListActivity.this.dialogLoading.dismiss();
					SearchCookListActivity.this.ll_no_network.setVisibility(0);
				}
			});
			return;
		}
		this.ll_no_network.setVisibility(0);
	}

	private void initAdapter(List<CookResult> list) {
		this.adapter.setBreed(this.et_search_content.getText().toString());
		this.adapter.setLocation(this.paramCoordinate);
		this.adapter.setList(list);
		this.adapter.notifyDataSetChanged();
		this.lv_search_cook.hideFooterView(true);
	}

	private String getUrl() {
		return "http://www.idachu.cn/api/kitchener/search_word?page=" + this.page + "&keyword="
				+ this.et_search_content.getText().toString();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_no_network /* 2131165232 */:
			getData();
		case R.id.img_et_content_clear /* 2131165407 */:
			this.et_search_content.setText("");
			this.ll_no_data.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.lv_search_cook.hideFooter();
			initAdapter(null);
		case R.id.tv_clear /* 2131165408 */:
			finish();
		default:
		}
	}
}
